package www.truongnha.com;

import java.util.HashMap;

import Config.setting;
import TruongNhaLib.DatabaseHandler;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

// This activity connect to sqlite db to get user infomation:
public class Profile extends ListActivity{
	
    // Login Table Columns names
    private static final String KEY_ID = "id";
    private static final String KEY_LAST_NAME = "lastname";
    private static final String KEY_FIRST_NAME = "firstname";
    private static final String KEY_TYPE = "type";
    private static final String KEY_CLASS = "class";
    private static final String KEY_SCHOOL = "school";
    private static final String KEY_BIRTH = "birth";
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
	  super.onCreate(savedInstanceState);
	  setContentView(R.layout.profile);
	  //Create db object:
	  Log.d("LOG", "Post PROFILE 0" );
	  DatabaseHandler db = new DatabaseHandler(getApplicationContext());
	  //Get user infomation from db to hashmap:
	  HashMap<String, String> userDetailsHash = db.getUserDetails();
	  String[] userDetails = new String[] {"type: ","Trường: "+userDetailsHash.get(KEY_SCHOOL),"Họ tên: "+userDetailsHash.get(KEY_LAST_NAME) +" "+ userDetailsHash.get(KEY_FIRST_NAME) ,
			  "Lớp: "+userDetailsHash.get(KEY_CLASS),"Ngày sinh: "+userDetailsHash.get(KEY_BIRTH)};
	  if(userDetailsHash.get(KEY_TYPE).equals("3")){ // 3 <=> teacher
		  userDetails[0]="Tài khoản Giáo viên" ;
	  } else{ // 1<=> student
		  userDetails[0]="Tài khoản Học sinh" ;
	  }
	  setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, userDetails));
	  Log.d("LOG", "Post PROFILE 5"+userDetails.toString() );
	  ListView lv = getListView();
	  Log.d("LOG", "Post PROFILE 6" );
	  final Toast toast = Toast.makeText(getApplicationContext(), "", Toast.LENGTH_SHORT);
	  lv.setOnItemClickListener(new OnItemClickListener() {
	    public void onItemClick(AdapterView<?> parent, View view,
	        int position, long id) {
	    	 Log.d("LOG", "Post PROFILE 7" );
	    	 //Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
	         //Toast.LENGTH_SHORT).show();
	    	 //toast.cancel();
	    	 toast.setText( ((TextView) view).getText() );
	    	 toast.show();
	    }
	  });
	  }
	/**
	 * Press back button twice to exit
	 */
	private long lastPressedTime;
	private Toast toast;
	public boolean onKeyDown(int keyCode, KeyEvent event) {
	    if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
	        switch (event.getAction()) {
	        case KeyEvent.ACTION_DOWN:
	            if (event.getDownTime() - lastPressedTime < setting.PERIOD_TWICE_TO_EXIT) {
	            	toast.cancel();
	                finish();
	            } else {
	            	toast = Toast.makeText(getApplicationContext(), "", Toast.LENGTH_SHORT);
	            	toast.setText("Nhấn back một lần nữa để thoát | profile");
	            	toast.show();
	                lastPressedTime = event.getEventTime();
	            }
	            return true;
	        }
	    }
	    return false;
	}
}
